/*
 * acm1002.c
 *
 *  Created on: 24 Mar 2010
 *      Author: xuxu
 */

#include<stdio.h>

int count(char num[4][4],int i,int j,int n)
{
	int bo=0;
	int ti,tj;
	if((i==n)&&(j==0))
		return 0;
	if('X'==num[i][j])
	{
		return count(num,i+(j+1)/n,(j+1)%n,n);
	}

	ti=i;
	tj=j;
	while(ti>=0)
	{
		if('0'==num[ti][j])
		{
			bo=1;
			break;
		}
		if('X'==num[ti][j])
			break;
		ti--;

	}
	while(tj>=0)
	{
		if('0'==num[i][tj])
		{
			bo=1;
			break;
		}
		if('X'==num[i][tj])
				break;
		tj--;
	}
	if(bo)
		return count(num,i+(j+1)/n,(j+1)%n,n);

	num[i][j]='0';
	ti=1+count(num,i+(j+1)/n,(j+1)%n,n);
	num[i][j]='.';
	tj=count(num,i+(j+1)/n,(j+1)%n,n);
	return ti>tj?ti:tj;

}

int main()
{
	char num[4][4];
	int n;
	int i,j;
	while(scanf("%d%*c",&n)&&n)
	{
		for(i=0;i<n;i++)
		{
			for(j=0;j<n;j++)
				scanf("%c",&num[i][j]);
			scanf("%*c");
		}

		printf("%d\n",count(num,0,0,n));
	}
	return 0;
}
